package com.atguigu.java2;

/**
 * @author : leimingchao
 * @date Date : 2021年08月22日 0:09
 */
public class StudentTest {

    public static void main(String[] args) {
        try {
            Student  student = new Student();
            student.register(-1001);
            System.out.println(student);
        } catch (Exception e) {
            // e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}


class Student {

    private int id;

    public void register(int id) throws Exception {
        if (id > 0) {
            this.id = id;
        } else {
            // System.out.println("你输入的数据非法！");
            //手动抛出异常对象
            // throw new RuntimeException("你输入的数据非法！");
            // throw new Exception("你输入的数据非法！");
            throw new MyException("不能输入负数");

            //错误的
            // throw new String("不能输入负数");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                '}';
    }
}